package Views;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Models.ServerManagementModel;

import javax.swing.JLabel;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ServerStatusView extends JFrame implements Observer {
	private JLabel alert;
	private ServerManagementModel model;
	private JLabel lblActif;
	private JTextArea textAreaRequest; 
	
	public ServerStatusView(ServerManagementModel model, ActionListener controller, String name) {
		this.model = model;
		model.addObserver(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 350);
		JPanel contentPane = new JPanel();
		contentPane.setBackground(new Color(204, 204, 255));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		contentPane.setLayout(null);
		setTitle(name);
		
		JButton buttonStop = new JButton("Stop");
		buttonStop.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonStop.setBounds(10, 284, 109, 22);
		contentPane.add(buttonStop);	
		buttonStop.addActionListener(controller);
		buttonStop.setActionCommand("Stop");
			
		setContentPane(contentPane);
		
		JLabel lblLogin = new JLabel("Gestion du serveur");
		lblLogin.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblLogin.setBounds(10, 11, 306, 22);
		contentPane.add(lblLogin);
		
		alert = new JLabel("");
		alert.setForeground(Color.RED);
		alert.setBounds(10, 255, 364, 22);
		contentPane.add(alert);
		
		lblActif = new JLabel("");
		lblActif.setBounds(59, 44, 46, 14);
		contentPane.add(lblActif);
		
		JLabel lblHistorique = new JLabel("Historique des commandes des utilisateurs :");
		lblHistorique.setBounds(10, 44, 320, 14);
		contentPane.add(lblHistorique);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 69, 364, 175);
		contentPane.add(scrollPane);
		
		textAreaRequest = new JTextArea();
		textAreaRequest.setEditable(false);
		textAreaRequest.setEnabled(false);
		textAreaRequest.setLineWrap(true);
		textAreaRequest.setWrapStyleWord(true);
		textAreaRequest.setDisabledTextColor(Color.BLACK);
		scrollPane.setViewportView(textAreaRequest);
	
		setVisible(true);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		alert.setText(model.getAlert());		

		String clientRequest = model.getClientRequest();
		String text = clientRequest + "\n" + textAreaRequest.getText();
		
		textAreaRequest.setText(text);	
	}
}
